﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Peculiar
{
    public partial class SubmitDataControl : BasePage
    {

        Dictionary<string, string> hid ;//存储当前页面勾选情况
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 
                banding();
            }
        }
        public SubmitDataControl() 
        {
            this.ModuleNo = "特殊权限";
            this.PageOperateType = "数据提交控制";
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            banding();
        }
        private void banding()
        {
            StringBuilder where = new StringBuilder();

            if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
                //中文按Produtname查询，英文按照PinyinCode查询
                if (HZ.Common.zhHansUtil.HaveChineseCode(this.txtProductName.Text.Trim()))
                {
                    where.Append(" and ProductName like '%" + txtProductName.Text.Trim() + "%'");
                }
                else
                {
                    where.Append(" and PinyinCode like '%" + txtProductName.Text.Trim() + "%'");
                }
            }
            if (!string.IsNullOrEmpty(txtMedicineModelName.Text.Trim()))
            {
                where.Append(" and MedicineModelName = '" + txtMedicineModelName.Text.Trim() + "'");
            }
            if (ddlSortSource.SelectedValue != "-1")
            {
                where.Append(" and SortSource = '" + ddlSortSource.SelectedValue + "'");
            }
            if (!string.IsNullOrEmpty(txtOutlookc.Text.Trim()))
            {
                where.Append(" and Outlookc = '" + txtOutlookc.Text.Trim() + "'");
            }
            
            if (!string.IsNullOrEmpty(txtCompanyName_SC.Text.Trim()))
            {
                where.Append(" and tCompanyName_SC  like '%" + txtCompanyName_SC.Text.Trim() + "%'");
            }
            if (!droSort.SelectedItem.Text.Trim().Equals("全部"))
            {
                where.Append(" and IsShowProvince='" + droSort.SelectedValue + "'");
            }
            int totalrecord = 0;
            HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
            DataTable dt = sp.findProcureCatalog(where.ToString(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
            hid = new Dictionary<string, string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                hid.Add(dt.Rows[i]["ProcureCatalogID"].ToString(), dt.Rows[i]["IsShowProvince"].ToString());
            }
            ViewState["hid"] = hid;
            
            gvwProduceCatalog.DataSource = dt;
            gvwProduceCatalog.DataBind();
            AspNetPager1.RecordCount = totalrecord;
            if (dt.Rows.Count == 0) btnSave.Visible = false;
            else btnSave.Visible = true;
        }
     
        protected void btnSave_Click(object sender, EventArgs e)
        {
           string strupdate1="",strupdate0="";
             string strid="";
            
            if( Request.Form["ChkID"]!=null)
            {
              strid = Request.Form["ChkID"].ToString();
            }
            hid = (Dictionary<string, string>)ViewState["hid"];
            foreach (string str in hid.Keys)
            {
                if(Function.IsContains(strid.Split(','),str))//如果包含
                {
                    if (hid[str].ToString().Equals("1"))//如果原来为1代表此条不需更新
                    { }
                    else//原来不是1，代表现在需要更新为1
                    {
                        strupdate1 += ",'" + str + "'";
                    }
                }
                else//不包含说明前台未选中，代表撤销此条更新为0
                {
                    if (hid[str].ToString().Equals("0"))//如果原来为1代表此条不需更新
                    { }
                    else//原来不是1，代表现在需要更新为1
                    {
                        strupdate0 += ",'" + str + "'";
                    }
                   
                }
            }
            string userAreaId = CurrentAccount.GetUser().UserID;
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
            if (string.IsNullOrEmpty(strupdate1) && string.IsNullOrEmpty(strupdate0))
            {
                HZ.Web.WebUtil.Alert("没有修改无需保存");
                return;
            }
            int nresult = iPrescription.UpdateSubmitDataControlID(strupdate1, strupdate0, userAreaId);
            if (nresult > 0)
            {
                Response.Write("<script>alert('保存成功！')</script>");
            }
            else
            {
                Response.Write("<script>alert('保存失败！')</script>");
            }
            banding();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            banding();
        }
    }
}

